from rest_framework import serializers
from django_redis import get_redis_connection

# 用户点击绑定按钮的时候,前端应该将 手机号,密码,openid,sms_code 发送给后端
from oauth.models import OAuthQQUser
from oauth.utils import check_open_id
from users.models import User


class OauthQQUserSerializer(serializers.Serializer):
    """
      QQ登录创建用户序列化器
      """
    access_token = serializers.CharField(label='操作凭证')
    mobile = serializers.RegexField(label='手机号', regex=r'^1[3-9]\d{9}$')
    password = serializers.CharField(label='密码', max_length=20, min_length=8)
    sms_code = serializers.CharField(label='短信验证码')

    def validate(self, attrs):
        # 2. 对数据进行校验
        #     2.1 校验 openid
        openid = check_open_id(attrs.get('access_token'))

        if openid is None:
            raise serializers.ValidationError('access_tocke错误')
        attrs['openid'] = openid

        # 校验sms_code
        sms_code = attrs.get('sms_code')
        redis_conn = get_redis_connection('code')
        redis_code = redis_conn.get('sms_%s' % attrs['mobile'])

        if redis_code is None:
            raise serializers.ValidationError("短信验证码已过期")

        if redis_code.decode() != sms_code:
            raise serializers.ValidationError("验证码不一致")
        # 2.2 判断手机号
        mobile = attrs.get("mobile")
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 如果没有注册过,创建用户
            pass


        else:
            # 如果注册过，需要判断，密码是否正确
            if not user.check_password(attrs['password']):
                raise serializers.ValidationError("密码不正确")

            attrs['user'] = user

        return attrs

    def create(self, validated_data):
        """
           最终要保存 user 和openid信息

        """
        user = validated_data.get("user")
        openid=validated_data.get("openid")
        if user is None:
            # 创建
            user =User.objects.create(

                username = validated_data.get('mobile'),
                mobile =validated_data.get('mobile'),
                password=validated_data.get('password')
            )
            # 对密码进行加密处理
            user.set_password(validated_data.get('password'))
            user.save()

        qquser=OAuthQQUser.objects.create(
            user=user,
            openid=openid
        )

        return qquser


